package com.bdqn.utli;

import org.junit.Test;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 忍
 * @title: 对信息进行加密
 * @projectName Teaching
 * @description: TODO
 * @date 2019/4/2415:00
 */
public class Encrypt {
    /***
     * MD5加码 生成32位md5码
     */
    public static String string2MD5(String inStr){
        MessageDigest md5 = null;
        try{
            md5 = MessageDigest.getInstance("MD5");
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++){
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();

    }

    /**
     * 加密解密算法 执行一次加密，两次解密
     */
    public static String convertMD5(String inStr){

        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++){
            a[i] = (char) (a[i] ^ '~');
        }
        String s = new String(a);
        return s;

    }
    /**
     * @description:  验证密码的正确
     * @param oldpwd  数据加密的数据
     * @param newpwd  用户输入
     * @return boolean
     * @throws
     * @author Mr.Zhou
     * @date 2019/5/16 21:21
     */
    public static boolean checkPwd(String newpwd ,String oldpwd){
        if(string2MD5(newpwd).equals(oldpwd))
            return true;
        else
            return false;
    }
    @Test
    public void test(){
        String pwd = "zhou1984523567.";
        String a= Encrypt.string2MD5(pwd);
        System.out.println(a);
        System.out.println(Encrypt.string2MD5(pwd));
        String b = Encrypt.convertMD5(a);
        System.out.println(b);
        String c = Encrypt.convertMD5(pwd);
        System.out.println(c);
    }
}
